跳到主要内容

NC103 反转字符串

https://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3

这题还是挺简单的,考察的其实是双指针,一个指向头,一个指向末尾。这样就只需要交换 n/2n/2 就行了

func solve( str string ) string {
runes := []rune(str)
for from, to := 0, len(runes) - 1; from < to; from, to = from + 1, to - 1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}